package com.hd.crdao.activity;

import android.content.Intent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import com.google.gson.Gson;
import com.hd.crdao.R;
import com.hd.crdao.adapter.ProvinceAdapter;
import com.hd.crdao.base.BaseSecondActivity;
import com.hd.crdao.bean.AreaListBean;
import com.hd.crdao.config.Constants;
import com.hd.crdao.utils.ALog;
import com.hd.crdao.utils.MyToast;
import com.hd.crdao.utils.SPUtils;
import com.hd.crdao.utils.UIUtils;
import com.hd.crdao.view.ExpandHeadView;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import butterknife.BindView;
import okhttp3.Call;

/**
 *
 *@author Administrator
 *@date 2017/5/26 0026
 */
public class SelectProvinceActivity extends BaseSecondActivity {

    @BindView(R.id.address_head)
    ExpandHeadView mAddressHead;
    @BindView(R.id.lv_address)
    ListView mLvAddress;
    public AreaListBean mAreaListBean;
    public String mProvince;


    @Override
    protected void initView() {
        initData();
        initListener();
    }

    @Override
    protected int setContentView() {
        return R.layout.activity_address;
    }

    @Override
    protected ExpandHeadView setExpandHeadView() {
        return mAddressHead;
    }


    protected void initListener() {
        mLvAddress.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                AreaListBean.ListAreaBean listAreaBean = mAreaListBean.getListArea().get(position);
                String parentId = listAreaBean.getId();
                String listAreaBeanId = listAreaBean.getId();
                mProvince = listAreaBean.getName();
                Intent intent = new Intent(UIUtils.getContext(), SelectCityActivity.class);
                intent.putExtra("parentId", parentId);
                intent.putExtra("province",mProvince);
                SPUtils.saveString("areaid",listAreaBeanId);
                startActivity(intent);
                finish();

            }
        });
    }


    protected void initData() {

        try {
            OkHttpUtils.post().url(Constants.CITYURL).addParams("type", "1").addParams("sign", Constants.SIGN).build().execute(new StringCallback() {
                @Override
                public void onError(Call call, Exception e, int id) {
                    ALog.e("请求数据失败,给出提示用户检查网络");
                    MyToast.show("获取数据失败,请检查网络...");
                }

                @Override
                public void onResponse(String response, int id) {
                    Gson gson = new Gson();
                    mAreaListBean = gson.fromJson(response, AreaListBean.class);
                    mLvAddress.setAdapter(new ProvinceAdapter(mAreaListBean.getListArea()));
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
        }

    }


}
